import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'dart:io';

Widget _buildMaterialDialogTransitions(
    BuildContext context,
    Animation<double> animation,
    Animation<double> secondaryAnimation,
    Widget child) {
  return FadeTransition(
    opacity: CurvedAnimation(
      parent: animation,
      curve: Curves.easeOut,
    ),
    child: child,
  );
}

class NavigatorUtils {
  static Future<T> showGSYDialog<T>({
    @required BuildContext context,
    bool barrierDismissible = true,
    Widget child,
  }) {
    return showDialog<T>(
        context: context,
        barrierDismissible: barrierDismissible,
        builder: (context) {
          return WillPopScope(
              child: MediaQuery(

                ///不受系统字体缩放影响
                  data:
                  MediaQueryData.fromWindow(WidgetsBinding.instance.window)
                      .copyWith(textScaleFactor: 1),
                  child: new SafeArea(child: child)),
              onWillPop: null);
        });
  }

  static showLoading<T>({@required BuildContext context, Widget child}) {
    showGeneralDialog(
      context: context,
      pageBuilder: (BuildContext buildContext, Animation<double> animation,
          Animation<double> secondaryAnimation) {
        final Widget pageChild = child;
        final ThemeData theme = Theme.of(context, shadowThemeOnly: true);
        return SafeArea(
          child: Builder(builder: (BuildContext context) {
            return theme != null
                ? Theme(data: theme, child: pageChild)
                : pageChild;
          }),
        );
      },
      barrierDismissible: false,
      barrierLabel: MaterialLocalizations.of(context).modalBarrierDismissLabel,
      barrierColor: Colors.black12,
      transitionDuration: const Duration(milliseconds: 150),
      transitionBuilder: _buildMaterialDialogTransitions,
    );
  }

  static Future showComfirmDialog({BuildContext context, Widget child}) {
    if (Platform.isAndroid) {
      return showDialog<int>(
        context: context,
        builder: (BuildContext context) => child,
      );
    } else {
      return showCupertinoDialog<int>(
        context: context,
        builder: (BuildContext context) => child,
      );
    }
  }

  static Future showActionSheet({BuildContext context, Widget child}) {
    return showCupertinoModalPopup<int>(
      context: context,
      builder: (BuildContext context) => child,
    );
  }

  static Future showCupertinonDatePicker ({BuildContext context, Widget child}) {
    return showCupertinoModalPopup<void>(
      context: context,
      builder: (BuildContext context) => child,
    );
  }
}
